package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author Sea
 * @date 2023/3/5 16:27
 */
@Api(tags = "菜品列表")
@Cacheable
@RestController
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("菜品分页")
    // 菜品列表
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String name) {// 1.接收参数
        // 2.调用service查询
        Page<Dish> dishPage = dishService.findByPage(pageNum, pageSize, name);
        // 3.返回结果
        return ResultInfo.success(dishPage);
    }
    @ApiOperation("新增菜品")
    // 新增菜品

    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){
        dishService.save(dish);
        //清理缓存
        String dishKey="dish_"+dish.getCategoryId();
        redisTemplate.delete(dishKey);
        return ResultInfo.success();
    }
    @ApiOperation("回显菜品")
    // 回显菜品
    @GetMapping("/dish/{dishId}")
    public ResultInfo findById(@PathVariable("dishId") Long dishId) {

        return ResultInfo.success(dishService.findById(dishId));
    }




    //抽取删除公共方法
    private void clean(String key){
        Set keys =redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }
    @ApiOperation("修改菜品")
    // 修改菜品
   @PutMapping("/dish")
    public ResultInfo updateDish(@RequestBody Dish dish) {
        dishService.updateDish(dish);
        //清理缓存
       clean("dish_*");
        return ResultInfo.success();
    }




    @ApiOperation("起售停售")
    // 起售 停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status")Integer status, @RequestParam List<Long>ids){
        dishService.updateStatus(status,ids);
        clean("dish_*");
        return ResultInfo.success();
    }
    @ApiOperation("菜品删除")
    //菜品删除
    @DeleteMapping("/dish")
    public ResultInfo deleteDish(@RequestParam List<Long>ids){
        dishService.deleteDish(ids);
        return ResultInfo.success();

    }
    @ApiOperation("查询菜品列表")
    //根据分类查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findListBySetmeal(Long categroyId,String name){
      List<Dish>dishList= dishService.findListBySetmeal(categroyId,name);
    return  ResultInfo.success(dishList);
    }
}
